const path = require('path');
const {defineConfig} = require('@vue/cli-service');
const TerserPlugin = require('terser-webpack-plugin')
const FileManagerPlugin = require('filemanager-webpack-plugin');

const variable = require('./variables')
const lessVars = {}
Object.keys(variable).map(key => lessVars[`@${key.replace(/([A-Z])/g, char => '-' + char.toLowerCase())
}`] = variable[key])

console.log("less all variables:")
console.log(lessVars)


module.exports = defineConfig({
    publicPath: '/',
    productionSourceMap: false,
    transpileDependencies: true,
    css: {
        loaderOptions: {
            less: {
                globalVars: lessVars
            }
        }
    },
    chainWebpack: config => {
        config.plugin('extract-css').tap(args => {
            args[0].filename = 'css/vue-x-split-panel.min.css'; // 定义 CSS 文件名
            args[0].chunkFilename = 'css/vue-x-split-panel.min.css'; // 定义动态导入的 CSS 文件名
            return args;
        });
    },
    configureWebpack: {
        entry: './src/index.js',
        output: {
            path: path.join(__dirname, 'dist'),
            filename: 'vue-x-split-panel.min.js',
            library: 'VueXSplitPanel',
            libraryExport: 'default',
            libraryTarget: 'umd',
            umdNamedDefine: true,
        },
        externals: {
            vue: {
                root: 'Vue',
                commonjs: 'vue',
                commonjs2: 'vue',
                amd: 'vue'
            }
        },
        optimization: {
            splitChunks: {
                chunks: 'all',
                minSize: Number.MAX_VALUE, // 设置一个很大的值，这样就不会有模块被分割出来
            },
            minimizer: [
                new TerserPlugin({
                    terserOptions: {
                        mangle: true, // 混淆
                        compress: {
                            drop_console: true, // 传true就是干掉所有的console.*这些函数的调用.
                            drop_debugger: true, // 干掉那些debugger;
                            pure_funcs: ['console.log', 'console.info', 'console.warn', 'console.error'] // 如果你要干掉特定的函数比如console.info ，又想删掉后保留其参数中的副作用，那用pure_funcs来处理
                        },
                        sourceMap: true, // 如果你希望为压缩后的代码生成sourceMap
                        // parallel: true, // 使用多进程并行运行来提高构建速度
                    }
                })
            ]
        },
        plugins: [
            new FileManagerPlugin({
                events: {
                    onEnd: {
                        mkdir: ['build/vue-x-split-panel'],
                        delete: [
                            {
                                source: './build/vue-x-split-panel*',
                                options: {
                                    force: true,
                                },
                            },
                        ],
                        copy: [
                            {source: './README.md', destination: './dist/build/vue-x-split-panel/'},
                            {source: './dist/*.js', destination: './dist/build/vue-x-split-panel/lib/'},
                            {source: './dist/css/*', destination: './dist/build/vue-x-split-panel/lib/css/'},
                            {source: './build/package.json', destination: './dist/build/vue-x-split-panel/'},
                        ],
                        archive: [
                            {source: './build', destination: './dist/build/vue-x-split-panel.zip'},
                        ]
                    }
                }
            })
        ],
        resolve: {
            extensions: ['.js', '.vue', '.less'],
            alias: {
                '@': path.join(__dirname, 'src')
            }
        }
    },
});
